package com.zmn.oms.dubbo.impl.normal.order;

import com.alibaba.fastjson.JSON;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.dio.normal.acceptance.OrderAcceptanceDIO;
import com.zmn.oms.common.dro.normal.acceptance.OrderAcceptanceDRO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.dubbo.interfaces.normal.order.OrderAcceptanceRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.dto.acceptance.OrderWorkAcceptanceDTO;
import com.zmn.oms.model.entity.acceptance.OrderWorkAcceptance;
import com.zmn.oms.model.entity.acceptance.OrderWorkAcceptanceDbQuery;
import com.zmn.oms.services.interfaces.acceptance.OrderWorkAcceptanceService;
import com.zmn.oms.zmn.business.interfaces.acceptance.OrderWorkAcceptanceBService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.Objects;


/**
 * 类描述：订单验收
 *
 * @author wangxiaokun
 * @since 2021/09/23 14:20
 */
@Slf4j
@Service(loadbalance = "roundrobin", timeout = 10000, retries = 0, version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class OrderAcceptanceRemoteServiceImpl implements OrderAcceptanceRemoteService {

    @Autowired
    private OrderWorkAcceptanceBService orderWorkAcceptanceBService;

    @Autowired
    private OrderWorkAcceptanceService orderWorkAcceptanceService;

    /**
     * 查询单个验收内容
     * @param acceptanceId
     * @return
     */
    @Override
    public ResponseDTO<OrderAcceptanceDRO> getOrderWorkAcceptanceByAcceptanceIdAndOrderId(Long acceptanceId, Long orderId) {
        log.info("查询单个验收入参：{}", JSON.toJSONString(acceptanceId));
        try {
            OrderWorkAcceptance acceptance = orderWorkAcceptanceService.findOrderWorkAcceptanceByKeySrcMaster(acceptanceId, orderId);
            OrderAcceptanceDRO dro = BeanMapper.map(acceptance, OrderAcceptanceDRO.class);
            return ResponseDTO.success(dro);
        } catch (Exception e) {
            log.error("查询单个验收异常：[{}]", e.getMessage());
            e.printStackTrace();
            return ResponseDTO.fail();
        }
    }

    @Override
    public ResponseDTO<List<OrderAcceptanceDRO>> listOrderWorkAcceptanceByOrderId(Long orderId, Long workId) {
        try {
            OrderWorkAcceptanceDbQuery query = new OrderWorkAcceptanceDbQuery();
            query.setOrderId(orderId);
            query.setWorkId(workId);
            List<OrderWorkAcceptance> list = orderWorkAcceptanceService.listOrderWorkAcceptanceByQuerySrcMaster(query);
            List<OrderAcceptanceDRO> resultList = BeanMapper.mapList(list, OrderAcceptanceDRO.class);
            return ResponseDTO.success(resultList);
        } catch (Exception e) {
            log.error("查询多个验收异常：[{}]", e.getMessage());
            e.printStackTrace();
            return ResponseDTO.fail();
        }
    }

    /**
     * 订单验收
     * @param orderAcceptanceDIO
     * @return
     */
    @Override
    public ResponseDTO modifyOrderWorkAcceptance(OrderAcceptanceDIO orderAcceptanceDIO) {
        log.info("修改订单验收入参：{}", JSON.toJSONString(orderAcceptanceDIO));
        try {
            OrderWorkAcceptanceDTO dto = BeanMapper.map(orderAcceptanceDIO, OrderWorkAcceptanceDTO.class);
            if (Objects.equals(dto.getAcceptanceResultStatus(), 6)) {
                dto.setAcceptanceResultStatus(OrderConsts.ORDER_ACCEPTANCE_STATUS_SUCCESS);
            }
            dto.setWorkId(dto.getOrderId());
            dto.setOperateTime(DateUtil.getNow());
            orderWorkAcceptanceBService.modifyOrderWorkAcceptance(dto);
            return ResponseDTO.success();
        } catch (OmsBaseException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            log.error("订单验收异常：[{}]", e);
            e.printStackTrace();
            return ResponseDTO.fail(e.getMessage());
        }
    }

}
